package com.dyz.leetcode;

import java.util.Arrays;
import java.util.PriorityQueue;

public class MinMeetingRoom253 {
    public int minMeetRooms(int[][] intervals){
        //corner case
        if(intervals==null||intervals.length==0){return 0;}
        Arrays.sort(intervals, (int[] a, int[] b)->( a[0]-b[0]));
        PriorityQueue<int[]> pq = new PriorityQueue<>((int[]a, int[]b)->(a[1]-b[1]));
        pq.offer(intervals[0]);
        //上一个结束时间，和当前开始时间的比较
        for(int i=1; i<intervals.length;i++){
            if(intervals[i][0]>=pq.peek()[1]){
                pq.poll();
            }
            pq.offer(intervals[i]);
        }
        return pq.size();
    }
    public int minMeetRooms1(int[][] intervals){

        int[] start = new int[intervals.length];
        int[] end = new int[intervals.length];
        for (int i=0; i<intervals.length; i++ ){
            start[i]=intervals[i][0];
            end[i]=intervals[i][1];
        }
        Arrays.sort(start);
        Arrays.sort(end);
        int id =0;
        int res = 0;
        for (int i=0; i<intervals.length;++i){
            if(end[id]>start[i]){
                res++;
            }else {
                id++;
            }
        }
        return res;
    }
}
